package com.labmi.experiment;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

@Entity
public class Color {

	@Id
	private String hexCode;
	
	private String name;
	
	@OneToMany(mappedBy="color", cascade={CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
	private List<ItemStock> itemStockList;
	
	public String getHexCode() {
		return hexCode;
	}

	public void setHexCode(String hexCode) {
		this.hexCode = hexCode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setItemStockList(List<ItemStock> itemStockList) {
		this.itemStockList = itemStockList;
	}

	public List<ItemStock> getItemStockList() {
		if(itemStockList == null)
			itemStockList = new ArrayList<ItemStock>(); //Lazy instantiation
		
		return itemStockList;
		
	}

	
	public void addItemStock(ItemStock itemStock) {
		if(itemStock != null)
		{
			if(itemStockList == null)
				itemStockList = new ArrayList<ItemStock>();
			
			this.itemStockList.add(itemStock);
			
			if(itemStock.getColor() != this )			
				itemStock.setColor(this);
		}
			
	}

	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Size)
		{
			return 
				this.hexCode.equals(((Color) obj).getHexCode());
		}
		
		return false;
	}
	
	@Override
	public int hashCode() {
		return hexCode.hashCode();
	}
	
	
}
